package controller.v2;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import service.v2.ConfigService;
import util.FileUtil;
import util.PackageUtil;
import util.StringUtil;

@Controller
@RequestMapping("/v2")
public class AppUpdateController {
	
	@Autowired
	private ConfigService configService;
	@ResponseBody
	@RequestMapping(value="/appUpdate",method=RequestMethod.GET) 
	public Object appUpdate(HttpServletRequest request, HttpServletResponse response,String version,@RequestHeader (value="OS", required=false) String os){
		if( (!"ios".equals(os.toLowerCase())) && !"android".equals(os.toLowerCase())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(version == null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		Map map = new HashMap();
		Map updateMap = configService.getUpdate(version);
		//当前版本号
		map.put("version", updateMap.get("version"));
		map.put("content", updateMap.get("content"));
		map.put("force", updateMap.get("force"));
		if("ios".equals(os.toLowerCase())){
			map.put("url", updateMap.get("url_ios"));
		}else if("android".equals(os.toLowerCase())){
			map.put("url", updateMap.get("url_android"));
		}
//		map.put("version", configService.loadConfig("APP_VERSION"));
//		map.put("content", configService.loadConfig("APP_UPDATE_CONTENT"));
//		map.put("force", configService.loadConfig("APP_FORCE"));
//		if("ios".equals(os.toLowerCase())){
//			map.put("url", configService.loadConfig("APP_DOWNLOAD_URL_IOS"));
//		}else if("android".equals(os.toLowerCase())){
//			map.put("url", configService.loadConfig("APP_DOWNLOAD_URL_ANDROID"));
//		}
		return PackageUtil.packageParams(200,"successs",map,""); 
	}

}
